﻿namespace uTorrentNAS
{
    using System.Collections.Generic;
    using System.Linq;

    public class ConfigurationSource
    {
        private readonly IList<UTorrentSettings> _clientSettings;
        private readonly IList<ScannerSettings> _scannerSettings;
        private readonly IList<Preferences> _preferences;

        private readonly IList<LabelItem> _labelItems;

        public ConfigurationSource(Configuration configuration)
        {
            _clientSettings = new List<UTorrentSettings>() { configuration.UTorrentSettings };
            _scannerSettings = new List<ScannerSettings>() { configuration.ScannerSettings };
            _preferences = new List<Preferences>() { configuration.Preferences };
            _labelItems = new List<LabelItem>(configuration.Preferences.LabelItems);
        }

        public IList<UTorrentSettings> GetUTorrentSettings()
        {
            return _clientSettings;
        }

        public IList<ScannerSettings> GetScannerSettings()
        {
            return _scannerSettings;
        }

        public IList<Preferences> GetPreferences()
        {
            return _preferences;
        }

        public IList<LabelItem> GetLabelItem(string name)
        {
            return _labelItems
                .Where(li => li.Name == name)
                .ToList();
        }

        public IList<LabelItem> GetLabels()
        {
            return _labelItems;
        }

        public Configuration GetConfiguration()
        {
            var config = new Configuration()
            {
                UTorrentSettings = _clientSettings[0],
                Preferences = _preferences[0],
                ScannerSettings = _scannerSettings[0]
            };
            config.Preferences.LabelItems = _labelItems;
            return config;
        }

        public void AddLabelItem(LabelItem item)
        {
            _labelItems.Add(item);
        }

        public void DeleteLabelItem(string name)
        {
            var item = _labelItems
                .FirstOrDefault(li => li.Name == name);
            if (item != null) _labelItems.Remove(item);
        }

        public bool IsLabelExist(string name)
        {
            return _labelItems.Any(li => li.Name == name);
        }

        public void MoveUpLabelItem(string name)
        {
            var item = _labelItems
                .FirstOrDefault(li => li.Name == name);
            var index = _labelItems.IndexOf(item);

            if (index > 0)
            {
                _labelItems.RemoveAt(index);
                _labelItems.Insert(index - 1, item);
            }
        }

        public void MoveDownLabelItem(string name)
        {
            var item = _labelItems
                .FirstOrDefault(li => li.Name == name);
            var index = _labelItems.IndexOf(item);

            if (index < _labelItems.Count - 1)
            {
                _labelItems.RemoveAt(index);
                _labelItems.Insert(index + 1, item);
            }
        }
    }
}
